﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Article_management_system
{
    public partial class Signin : Form
    {
        public Signin()
        {
            InitializeComponent();
        }
        bool exit_signin = true;//退出提示框处理使用
        private void button1_Click(object sender, EventArgs e)//注册按钮
        {
            if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text) && string.IsNullOrEmpty(textBox3.Text))
            {
                label8.Text = "用户名不能为空";
                label9.Text = "密码不能为空";
                label10.Text = "确认密码不能为空";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
            {
                label8.Text = "用户名不能为空";
                label9.Text = "密码不能为空";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox3.Text))
            {
                label8.Text = "用户名不能为空";
                label10.Text = "确认密码不能为空";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox2.Text) && string.IsNullOrEmpty(textBox3.Text))
            {
                label9.Text = "密码不能为空";
                label10.Text = "确认密码不能为空";
                textBox2.Focus();
            }
            else if (string.IsNullOrEmpty(textBox1.Text))
            {
                label8.Text = "用户名不能为空";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox2.Text))
            {
                label9.Text = "密码不能为空";
                textBox2.Focus();
            }
            else if (string.IsNullOrEmpty(textBox3.Text))
            {
                label10.Text = "确认密码不能为空";
                textBox3.Focus();
            }
            else
            {
                if (Sqldt.Repetition(textBox1.Text))
                {
                    label8.Text = "用户名已存在";
                    textBox1.SelectAll();
                    textBox1.Focus();
                }
                else if (textBox2.Text.Length < 6)
                {
                    label9.Text = "密码不足6位";
                    textBox2.Focus();
                }
                else if (!textBox2.Text.Equals(textBox3.Text))
                {
                    label10.Text = "两次密码不一致";
                    textBox3.Focus();
                }
                else
                {
                    string sql = $"INSERT INTO Users (Username,Password) VALUES ('{textBox1.Text}','{textBox3.Text}')";
                    Sqldt.Judge(sql);
                    var hint = MessageBox.Show($"注册成功！\n现可使用用户名{textBox1.Text}登录", "注册", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (hint.Equals(DialogResult.OK))
                    {
                        exit_signin = false;
                        this.Close();
                    }
                    else
                    {
                        textBox1.Text = null;
                        textBox2.Text = null;
                        textBox3.Text = null;
                        textBox1.Focus();
                        label8.Text = null;
                        label9.Text = null;
                        label10.Text = null;
                    }

                }
            }
                    timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)//取消按钮
        {
            this.Close();
        }

        private void Signin_FormClosing(object sender, FormClosingEventArgs e)//退出提示
        {
            if (exit_signin)
            {
                var hint = MessageBox.Show($"是否取消本次注册！", "取消", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (!hint.Equals(DialogResult.OK))
                {
                    e.Cancel = true;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)//用于提示自动消失
        {
            label8.Text = null;
            label9.Text = null;
            label10.Text = null;
            timer1.Enabled = false;
        }
    }
}
